package com.wxtwxt.ruralmall.product.service.impl;

import com.wxtwxt.ruralmall.product.entity.AttrEntity;
import com.wxtwxt.ruralmall.product.service.AttrService;
import com.wxtwxt.ruralmall.product.vo.BaseAttrs;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxtwxt.ruralmall.common.utils.PageUtils;
import com.wxtwxt.ruralmall.common.utils.Query;

import com.wxtwxt.ruralmall.product.dao.ProductAttrValueDao;
import com.wxtwxt.ruralmall.product.entity.ProductAttrValueEntity;
import com.wxtwxt.ruralmall.product.service.ProductAttrValueService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {

    @Resource
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveProductAttrValue(Long id, List<BaseAttrs> baseAttrs) {
        if(baseAttrs.size() > 0) {
            List<ProductAttrValueEntity> attrValueEntities = baseAttrs.stream().map(baseAttr -> {
                ProductAttrValueEntity productAttrValueEntity = new ProductAttrValueEntity();
                productAttrValueEntity.setSpuId(id);
                productAttrValueEntity.setAttrId(baseAttr.getAttrId());
                AttrEntity attrEntity = attrService.getById(baseAttr.getAttrId());
                productAttrValueEntity.setAttrName(attrEntity.getAttrName());
                productAttrValueEntity.setAttrValue(baseAttr.getAttrValues());
                productAttrValueEntity.setQuickShow(baseAttr.getShowDesc());
                return productAttrValueEntity;
            }).collect(Collectors.toList());
            this.saveBatch(attrValueEntities);
        }
    }

    @Override
    public List<ProductAttrValueEntity> getBaseListForSpu(Long spuId) {
        QueryWrapper<ProductAttrValueEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spu_id", spuId);
        return this.list(queryWrapper);
    }

    @Transactional
    @Override
    public void updateBaseForSku(Long spuId, List<ProductAttrValueEntity> list) {
        // 先把之前保存的属性全部删除
        this.remove(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
        // 再把前端返回的数据批量保存
        List<ProductAttrValueEntity> collect = list.stream().map(entity -> {
            entity.setSpuId(spuId);
            return entity;
        }).collect(Collectors.toList());
        this.saveBatch(collect);
    }

}